/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main_com.Controlers.massage;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import main_com.mapper.UserMapper;
import main_com.pojo.LoginBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/UpdatePasswordController")
public class UpdatePasswordController {
    //    public void wrong1(){
//        String msg="不允许有空，修改失败！";
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE);
//    }
//    public void wrong2(){
//        String msg="两次密码不同，修改失败！";
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE);
//    }
//    public void right(){
//        String msg="填写信息合格，修改成功！";
//        String title="信息提示";
//        JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE);
//    }
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/updatePassword")
    protected void updatePassword(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        if (password1.isEmpty() || password2.isEmpty()) {
//            wrong1();
            response.sendRedirect("http://localhost/lookMessage/updatePassword.jsp");
        } else if (!(password1.equals(password2))) {
//            wrong2();
            response.sendRedirect("http://localhost/lookMessage/updatePassword.jsp");
        } else {
            try {
                String userName = "";
                HttpSession session = request.getSession();
                ArrayList login = (ArrayList) session.getAttribute("login");
                if (login == null || login.isEmpty()) {
                    response.sendRedirect("http://localhost/login.jsp");
                } else {
                    for (int i = login.size() - 1; i >= 0; i--) {
                        LoginBean nn = (LoginBean) login.get(i);
                        userName = nn.getUserName();
                    }
                }
                userMapper.updatePasswordByUserName(userName, password1);
                LoginBean loginBean = userMapper.getLoginBeanByUserName(userName);
                ArrayList wordlist = null;
                wordlist = new ArrayList();
                wordlist.add(loginBean);
                session.setAttribute("login", login);
//                right();
                response.sendRedirect("http://localhost/lookMessage/lookMessage.jsp");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
